﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using NangLuongTieuDiemDTO.DTO;
using NangLuongTieuDiemDAO.DAO;

namespace NangLuongTieuDiemBUS.BUS
{
    public class ThietBiDienBUS
    {
        #region "[Insert]"
        public int Insert(ThietBiDienDTO tbdDto)
        {
            int IDResult = -1;
            try {
                IDResult = ThietBiDienDAO.Insert(tbdDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Insert", ex);
            }
            return IDResult;
        }
        #endregion

        #region "[Delete]"
        public bool Delete(int ID)
        {
            bool isSuccess = false;
            try { 
                isSuccess = ThietBiDienDAO.Delete(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Delete", ex);
            }
            return isSuccess;
        }
        #endregion

        #region "[Update]"
        public bool Update(ThietBiDienDTO tbdDto)
        {
            bool isSuccess = false;
            try
            {
                isSuccess = ThietBiDienDAO.Update(tbdDto);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("Update", ex);
            }
            return isSuccess;
        }
        #endregion

        #region "[Get all]"
        public List<ThietBiDienDTO> GetAll()
        {
            List<ThietBiDienDTO> listobjs = new List<ThietBiDienDTO>();
            try
            {
                listobjs = ThietBiDienDAO.GetAll();
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetAll", ex);
            }
            return listobjs;
        }
        #endregion

        #region "[SelectByPrimaryKey]"
        public ThietBiDienDTO SelectByPrimaryKey(int ID)
        {
            ThietBiDienDTO obj = null;
            try
            {
                obj = ThietBiDienDAO.SelectByPrimaryKey(ID);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("SelectByPrimaryKey", ex);
            }
            return obj;
        }

        #endregion

        #region "[Lấy danh sách thiết bị điện từ file excel]"
        /// <summary>
        /// Lấy danh sách thiết bị điện từ file excel
        /// </summary>
        /// <param name="_strLocation">đường dẫn đến file excel</param>
        /// <param name="_iSheet">tên sheet vd: sheet 1 thì là: 1</param>
        /// <returns>danh sách các đối tượng ThietBiDienDTO</returns>
        public List<ThietBiDienDTO> GetListFromFileExcel(string _strLocation, int _iSheet)
        {
            List<ThietBiDienDTO> listObj = new List<ThietBiDienDTO>();
            try {
                string strPath = "provider=Microsoft.Jet.OLEDB.4.0;Data Source='" + _strLocation + "';Extended Properties=Excel 8.0;";
                string strsql = "select * from [Sheet" + _iSheet.ToString() + "$]";
                System.Data.OleDb.OleDbConnection MyConnection;
                System.Data.DataSet DtSet;
                System.Data.OleDb.OleDbDataAdapter MyCommand;
                MyConnection = new System.Data.OleDb.OleDbConnection(strPath);
                MyCommand = new System.Data.OleDb.OleDbDataAdapter(strsql, MyConnection);
                MyCommand.TableMappings.Add("Table", "TestTable");
                DtSet = new System.Data.DataSet();
                MyCommand.Fill(DtSet);
                DataTable dt= DtSet.Tables[0];
                MyConnection.Close();
                if (dt.Rows.Count > 0)
                {
                    foreach (DataRow row in dt.Rows)
                    {
                        ThietBiDienDTO Item = new ThietBiDienDTO();
                        Item.TenThietBiDien = row["TenThietBiDien"].ToString();
                        Item.CongSuat = double.Parse(row["CongSuat"].ToString());
                        Item.CongSuatHaoHut = double.Parse(row["CongSuatHaoHut"].ToString());
                        Item.HeSoDeBa = double.Parse(row["HeSoDeBa"].ToString());
                        Item.Xoa = false;
                        Item.GhiChu = row["GhiChu"].ToString();

                        listObj.Add(Item);
                    }
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("GetListFromFileExcel", ex);
            }
            return listObj;
        }
        #endregion

        #region "[Insert danh sách các thiết bị điện xuống CSDL]"
        public ACK.SanPham_ThietBiDienACK InsertListThietBiDien(List<ThietBiDienDTO> _listInput)
        {
            ACK.SanPham_ThietBiDienACK listObj = new ACK.SanPham_ThietBiDienACK();
            try {
                listObj.Error = false;
                int i=0;
                foreach (ThietBiDienDTO ItemInsert in _listInput)
                {
                    int ID = Insert(ItemInsert);
                    if (ID == -1)
                    {
                        listObj.Error = true;
                        listObj.RowID.Add(i);
                    }
                    i++;
                }
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("InsertListThietBiDien", ex);
            }
            return listObj;
        }

        #endregion

        #region "[Tìm kiếm thiết bị điện]"//Doan Phu An - 1241302
        public List<ThietBiDienDTO> TimKiemThietBiDien(string ChuoiTimKiem)
        {
            List<ThietBiDienDTO> ds_tbd = new List<ThietBiDienDTO>(); 
            try
            {
                ds_tbd = ThietBiDienDAO.TimKiemThietBiDien(ChuoiTimKiem);
            }
            catch (Exception ex)
            {
                InSert_LoiHeThong("TimKiemThietBiDien", ex);
            }
            return ds_tbd;
        }
        #endregion

        #region "[Hàm insert lỗi hệ thống]"//Doan Phu An - 1241302
        public void InSert_LoiHeThong(string TenHam, Exception ex)
        {
            LoiHeThongDTO lhtDto = new LoiHeThongDTO();
            lhtDto.TenLoi = ex.Message.ToString();
            lhtDto.NgayTao = DateTime.Now;
            lhtDto.TenHamBiLoi = "ThietBiDienBUS_" + TenHam;
            lhtDto.SuaLoi = false;
            lhtDto.GhiChu = "";
            LoiHeThongDAO.Insert(lhtDto);
        }
        #endregion
    }
}
